package com.zmx.utils;

import com.alibaba.fastjson.JSON;
import com.zmx.advice.QuartzException;
import com.zmx.entity.ScheduleJob;
import com.zmx.quartz.MyJob;
import org.quartz.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 2020/10/21
 * 15:12
 * zmx
 */
public class ScheduleUtils {
    // 创建新任务
    public static void createTask(Scheduler scheduler, ScheduleJob scheduleJob){
        /* 1. 创建任务调度器*/
        // 2.创建触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withSchedule(CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()))
                .startNow()
                .withIdentity(QuartzConstant.TRIGGER_NAME+scheduleJob.getJobId()).build();
        // 3. JobDetail 创建
        JobDetail jobDetail =JobBuilder.
                newJob(MyJob.class)
                .withIdentity(QuartzConstant.JOB_NAME+scheduleJob.getJobId())
                .build();
        // 有些版本的Quartz ，不支持对象序列化 ，所以先转JSON再传输
        String json = JSON.toJSONString(scheduleJob);
        jobDetail.getJobDataMap().put(QuartzConstant.DATA_KEY, json);
        // 在    任务调度器中 注册 触发器 与任务
        try {
            scheduler.scheduleJob(jobDetail,cronTrigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
    // 删除定时任务
    public static void deleteTask(Scheduler scheduler, List<Long> ids){
        try {
            ArrayList<JobKey> jobKeys = new ArrayList<>();
            ids.forEach(v->{jobKeys.add(JobKey.jobKey(QuartzConstant.JOB_NAME+v));});
            scheduler.deleteJobs(jobKeys);

        } catch (SchedulerException e) {
            throw new QuartzException("删除任务失败");
        }
    }
    // 暂停定时任务
    public static void pauseTask(Scheduler scheduler,List<Long> ids){
        ArrayList<JobKey> jobKeys = new ArrayList<>();
        ids.forEach(v->{jobKeys.add(JobKey.jobKey(QuartzConstant.JOB_NAME+v));});
        jobKeys.forEach(v->{
            try {
                scheduler.pauseJob(v);
            } catch (SchedulerException e) {
                throw new QuartzException("暂停任务失败");
            }
        });
    }
    // 恢复任务
    public static void resumeTask(Scheduler scheduler,List<Long> ids){
        ArrayList<JobKey> jobKeys = new ArrayList<>();
        ids.forEach(v->{jobKeys.add(JobKey.jobKey(QuartzConstant.JOB_NAME+v));});
        jobKeys.forEach(v->{
            try {
                scheduler.resumeJob(v);
            } catch (SchedulerException e) {
                throw new QuartzException("恢复任务失败");
            }
        });
    }
    // 运行一次任务
    public static void runOnce(Scheduler scheduler,List<Long> ids){
        ArrayList<JobKey> jobKeys = new ArrayList<>();
        ids.forEach(v->{jobKeys.add(JobKey.jobKey(QuartzConstant.JOB_NAME+v));});
        jobKeys.forEach(v->{
            try {
                scheduler.triggerJob(v);
            } catch (SchedulerException e) {
                throw new QuartzException("运行一次任务失败");
            }
        });
    }
    // 任务修改 修改trigger
    public static void updateTask(Scheduler scheduler,ScheduleJob scheduleJob){
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(QuartzConstant.TRIGGER_NAME+scheduleJob.getJobId())
                .withSchedule(CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()))
                .build();
        try {
            scheduler.rescheduleJob(TriggerKey.triggerKey(QuartzConstant.TRIGGER_NAME+scheduleJob.getJobId()),cronTrigger);
        } catch (SchedulerException e) {
            throw new QuartzException("任务修改失败");
        }
    }
}
